<?php
/**
* Scribble Users.
*
* Copyright (c) 2009 James Gauld <james@jamesgauld.net>
* This file is part of Scribble.
* @license http://www.scribblecms.co.uk/license.txt
*
* @package Scribble
*/
use Buan\Model;
use Buan\ModelCriteria;
class UserModel extends ScribbleModel {

	/**
	* Encrypt the given password using this user's hash string.
	*
	* @param string Password to be encrypted
	* @return string
	*/
	public function encryptPassword($pw) {
		return md5($this->hashsalt.$pw);
	}

	/**
	* Generate a uniqie hash salt which can be assigned to a user.
	*
	* @return string
	*/
	public static function generateHashSalt() {
		return uniqid(rand());
	}

	/**
	* Generate a random, unique password for use by a user.
	*
	* @return string
	*/
	public static function generateRandomPassword() {
		return substr(uniqid(), 0, 10);
	}

	/**
	* Return an object containing all of this user's system preferences.
	*
	* @return StdClass
	*/
	public function getPreferences() {
		return new ScribbleUserPreferences($this);
	}

	/**
	* Test if this user is a member of the specified Role.
	*
	* @param RoleModel Role
	* @return bool
	*/
	public function isMemberOfRole($role) {
		$c = new ModelCriteria();
		$c->addClause(ModelCriteria::EQUALS, 'role_id', $role->id);
		return !$this->findRelatives('UserRole', $c)->isEmpty();
	}
}
?>